﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace webapp4U.UI
{
    public partial class ArticleAddEdit : System.Web.UI.Page
    {
        private int ArticleID = -1;
        private int CateID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            // lay ArticleID khi chinh sua
            ArticleID = Globals.GetIntFromQueryString("ArticleID");
            CateID = Globals.GetIntFromQueryString("CateID");
            if (Request.QueryString["Action"] == "Edit")
            {
                btSave.CommandArgument = "Edit";
                btSave.Text = " Update ";

                if (!IsPostBack)
                {
                    Load_GetItemByArticleID(ArticleID);
                }
            }
        }

        protected void Load_GetItemByArticleID(int ArticleID)
        {
            webapp4U.BOL.Article obj = webapp4U.BOL.Article.GetArticleByArticleID(ArticleID);
            txtTitle.Text = obj.Title;
            txtExcerpt.Text = obj.Excerpt;
            txtBody.Text = obj.Body;
            if (obj.Priority == 1)
                chkPriority.Checked = true;

            if(obj.ImageURL.Length>3)
                ImageItem.Src = Globals.ApplicationPath + "/UserUpload/Articles/" + obj.ImageURL;

            if (obj.ImageWidth > obj.ImageHeight)
                ImageItem.Width = 80;
            else
                ImageItem.Height = 80;

            dlArticleCategory.SelectedValue = obj.ArticleCateID.ToString();

            HiddenImageName.Value = obj.ImageURL;
            HiddenImageWidth.Value = obj.ImageWidth.ToString();
            HiddenImageHeight.Value = obj.ImageHeight.ToString();
        }


        protected void btSave_Click(object sender, EventArgs e)
        {
            webapp4U.BOL.Article obj = new webapp4U.BOL.Article();
            obj.ArticleID = ArticleID;
            obj.Title = txtTitle.Text.Trim();
            obj.Excerpt = txtExcerpt.Text.Trim().Replace(Environment.NewLine, "<br>");
            obj.Body = txtBody.Text;
            obj.PostDate = DateTime.Now;
            obj.Priority = 0;
            if (chkPriority.Checked)
                obj.Priority = 1;
            obj.ImageURL = HiddenImageName.Value.Trim();
            obj.ImageWidth = Convert.ToInt32(HiddenImageWidth.Value.Trim());
            obj.ImageHeight = Convert.ToInt32(HiddenImageHeight.Value.Trim());
            obj.Authors = " ";
            obj.ArticleCateID = Globals.ConvertToInt32(dlArticleCategory.SelectedItem.Value);

            if (btSave.CommandArgument == "Edit")
            {
                obj.TotalViews = 0;
                webapp4U.BOL.Article.Update(obj);
                //Response.Redirect("ArticleDetail.aspx?ArticleID=" + ArticleID);
            }
            else
            {
                webapp4U.BOL.Article.Insert(obj);
                
                Response.Redirect("Default.aspx?CateID=" + CateID);

                //txtBody.Text = "";
                //txtTitle.Text = "";
                //txtExcerpt.Text = "";
            }

            lblMsg.Text = "Cập nhật thông tin thành công!";

        }
    }
}